package com.woniuxy.hotel.user.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.woniuxy.hotel.user.pojo.User;
import com.woniuxy.hotel.user.service.MailService;
import com.woniuxy.hotel.user.service.UserService;
import com.woniuxy.hotel.user.util.RegexUtil;
import com.woniuxy.hotel.user.util.ResultDTO;

@Controller
@Scope("prototype")
@RequestMapping("/user")
public class UserHandler {

	@Autowired
	private UserService userservice;
	@Autowired
	private MailService mailService;

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public ModelAndView login(User user) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/login.html");
		if ((user.getAccount() == null || user.getAccount().length() == 0)
				&& (user.getPhone() == null || user.getPhone().length() == 0)) {
			modelAndView.addObject("tips", "请输入账号或手机号进行登录");
			return modelAndView;
		}
		if (user.getPassword() == null || user.getPassword().length() == 0) {
			modelAndView.addObject("account", user.getAccount() == null ? user.getPhone() : user.getAccount());
			modelAndView.addObject("tips", "请输入密码");
			return modelAndView;
		} else {
			try {
				String htmlURL = userservice.login(user);
				modelAndView.setViewName(htmlURL);
				return modelAndView;
			} catch (Exception e) {
				modelAndView.addObject("tips", e.getMessage());
				return modelAndView;
			}

		}
	}

	/**
	 * 
	 * 发送邮件找回密码
	 * 
	 * @param account
	 * @return
	 */
	@RequestMapping(value = "/rsByemail", method = RequestMethod.POST)
	@ResponseBody
	public ResultDTO sendEmail(String account) {
		ResultDTO resultDTO = new ResultDTO();
		if (account == null) {
			resultDTO.setCode(400);
			resultDTO.setMassage("账号不能为空");
			return resultDTO;
		}
		User user = userservice.findUserByAccount(account);
		if (user == null) {
			resultDTO.setCode(400);
			resultDTO.setMassage("账号不存在");
			return resultDTO;
		}
		if (user.getEmail() == null) {
			resultDTO.setCode(400);
			resultDTO.setMassage("没有绑定邮箱");
			return resultDTO;
		}
		try {
			String result = mailService.sendSimpleMail(user, "找回密码");
			resultDTO.setCode(200);
			resultDTO.setMassage(result);
			return resultDTO;
		} catch (RuntimeException e) {
			resultDTO.setCode(400);
			resultDTO.setMassage(e.getMessage());
		}
		return new ResultDTO();
	}

	/**
	 * 重置密码
	 * 
	 * @param account
	 * @param code
	 * @param password
	 * @return
	 */
	@RequestMapping(value = "/rsPassword")
	@ResponseBody
	public ResultDTO resetPassword(String account, String code, String password) {
		ResultDTO resultDTO = new ResultDTO();
		if (account == null) {
			resultDTO.setCode(200);
			resultDTO.setMassage("账号不能为空");
			return resultDTO;
		}
		String result = userservice.rsPassword(account, code, password);
		resultDTO.setCode(200);
		resultDTO.setMassage(result);
		return resultDTO;
	}

	/**
	 * 手机短信登录，发送验证码
	 * 
	 * @param phone
	 * @return
	 */
	@RequestMapping(value = "/loginCode", method = RequestMethod.POST)
	@ResponseBody
	public ResultDTO loginGetSMS(String phone) {
		ResultDTO resultDTO = new ResultDTO();
		if (!RegexUtil.isMobile(phone)) {
			resultDTO.setCode(400);
			resultDTO.setMassage("手机格式有误");
			return resultDTO;
		}
		try {
			String result = userservice.getLoginSMS(phone);
			resultDTO.setCode(200);
			resultDTO.setMassage(result);
			return resultDTO;
		} catch (RuntimeException e) {
			resultDTO.setCode(400);
			resultDTO.setMassage(e.getMessage());
			return resultDTO;
		}
	}

	@RequestMapping(value = "/loginByPhone", method = RequestMethod.POST)
	public ModelAndView loginByPhone(String phone, String code) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/login.html");
		if (!RegexUtil.isMobile(phone)) {
			modelAndView.addObject("tips", "手机格式有误");
			return modelAndView;
		}
		if (code.length() != 6) {
			modelAndView.addObject("tips", "验证码有误");
			return modelAndView;
		} else {
			try {
				String htmlURL = userservice.loginByPhone(phone, code);
				modelAndView.setViewName(htmlURL);
				return modelAndView;
			} catch (Exception e) {
				modelAndView.addObject("tips", e.getMessage());
				return modelAndView;
			}
		}
	}
}
